Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

25-H0ngJu #234

Merged
merged 1 commit into from
Aug 27, 2024
Merged

25-H0ngJu #234

merged 1 commit into from
Aug 27, 2024

Conversation

H0ngJu
Copy link
Collaborator

@H0ngJu H0ngJu commented Aug 20, 2024

๐Ÿ”— ๋ฌธ์ œ ๋งํฌ

์นœ๊ตฌ๋น„

โœ”๏ธ ์†Œ์š”๋œ ์‹œ๊ฐ„

40M

โœจ ์ˆ˜๋„ ์ฝ”๋“œ

image

๋ฌธ์ œ์š”์•ฝ
๊ฐ€์žฅ ์ ์€ ๋น„์šฉ์œผ๋กœ ๋ชจ๋“  ์‚ฌ๋žŒ๊ณผ ์นœ๊ตฌ๊ฐ€ ๋˜๋Š” ๋ฐฉ๋ฒ•์„ ๊ตฌํ•˜๋ผ@!
์นœ๊ตฌ A์—๊ฒŒ ์นœ๊ตฌ๋น„๋ฅผ ์ง€๋ถˆํ•˜๋ฉด, ์นœ๊ตฌ A์™€ ์นœ๊ตฌ๊ด€๊ณ„์ธ ์นœ๊ตฌ B๋„ ์นœ๊ตฌ๊ฐ€ ๋œ๋‹ค

์งค ์“ธ๋ ค๊ณ  ์ด ๋ฌธ์ œ ํ’€์—ˆ์Šต๋‹ˆ๋‹ค ใ…‹.ใ…‹



์‚ฌ์‹ค ์ฒ˜์Œ์— ๋ฌธ์ œ ๋ณด๊ณ  ์นœ๊ตฌ ๋„คํŠธ์›Œํฌ๊ฐ€ ์ƒ๊ฐ๋‚˜์„œ ์œ ๋‹ˆ์˜จ-ํŒŒ์ธ๋“œ๋กœ ํ’€ ์ˆ˜ ์žˆ์„ ๊ฒƒ ๊ฐ™์•„์„œ ์‹œ๋„ํ–ˆ๋Š”๋ฐ ํ’€๋‹ค๊ฐ€ ๋น ๋ฅด๊ฒŒ ๋Œ๋ ธ์Šต๋‹ˆ๋‹ค.

๋ถ€๋ชจ ๋…ธ๋“œ ์ถ”๊ฐ€ํ•  ๋•Œ๋งˆ๋‹ค ์ตœ์†Œ๋น„์šฉ์„ ์ €์žฅํ•˜๋ ค๊ณ  ํ–ˆ๋Š”๋ฐ, ์ž˜ ์•ˆ๋˜๋”๋ผ๊ณ ์š”.
์ถฉ๋ถ„ํžˆ ๊ฐ€๋Šฅํ•  ๊ฒƒ ๊ฐ™์€๋ฐ..



์–ด์จŒ๋“ , ์–ด๋–ค ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•˜๋“  ์นœ๊ตฌ๊ด€๊ณ„์ธ ์‚ฌ๋žŒ์„ ๋ชจ์•„๋‘๊ณ  ๊ทธ ์ค‘์— ์ตœ์†Œ๋ฅผ ์ฐพ์œผ๋ฉด ๊ทธ๋งŒ์ด๋‹ˆ๊นŒ deque๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๋ชจ์•„๋‘๊ธฐ๋กœ ํ–ˆ์Šต๋‹ˆ๋‹ค.

์นœ๊ตฌ๋Š” 5๋ช…, ์ค€์„์ด๊ฐ€ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ๋ˆ์€ 20,
5๋ช…์— ๋Œ€ํ•œ ๊ฐ๊ฐ ์นœ๊ตฌ๋น„๋Š” 10, 10, 20, 20, 30์ด๋ผ๊ณ  ๊ฐ€์ •ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.


๊ทธ๋ฆฌ๊ณ  1-3, 2-4, 4-5๊ฐ€ ์นœ๊ตฌ๊ด€๊ณ„์ธ ๊ฒฝ์šฐ๋ฅผ ์ƒ๊ฐํ•ด๋ด…์‹œ๋‹ค.
์ด๋•Œ, ์นœ๊ตฌ ๊ด€๊ณ„๋Š” relation์— ์ €์žฅํ•ฉ๋‹ˆ๋‹ค.

1-3์˜ ๊ฒฝ์šฐ,
relation[1]์—๋„ 3์„ ์ถ”๊ฐ€ํ•˜๊ณ , relation[3]์—๋„ 1์„ ์ถ”๊ฐ€ ํ•˜์—ฌ ์นœ๊ตฌ๊ด€๊ณ„๋ฅผ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค.

image
ใ„ด ์ €์žฅ๋œ relation ์ถœ๋ ฅ๊ฐ’




์ €์žฅ๋œ relation ์ •๋ณด๋ฅผ ๋ฐ”ํƒ•์œผ๋กœ,
๋จผ์ € 1๋ฒˆ ์นœ๊ตฌ๋ถ€ํ„ฐ ์‹œ์ž‘ํ•ด์„œ ๋ˆ„๊ตฌ์™€ ์นœ๊ตฌ ๊ด€๊ณ„์ธ์ง€ ํŒŒ์•…ํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค.

์ด๋•Œ q๋ฅผ ํ†ตํ•ด์„œ dfs๋กœ ํƒ์ƒ‰ํ•ด์ค๋‹ˆ๋‹ค.

๊ทธ๋ ‡๊ฒŒ ์นœ๊ตฌ ๊ด€๊ณ„์ธ N๋ช…์˜ ์‚ฌ๋žŒ๋“ค์„ friends๋ผ๋Š” ๋ฐฐ์—ด์— ๋„ฃ๊ณ , ์ตœ์†Œ ๋น„์šฉ์„ ๊ตฌํ•ด์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค.


๋ชจ๋“  ๊ด€๊ณ„์— ๋Œ€ํ•˜์—ฌ ์ตœ์†Œ๋น„์šฉ์„ ํ•ฉํ•œ ๊ฐ’์ธ total์ด k๊ฐ’์„ ๋„˜์œผ๋ฉด "oh no"๋ฅผ ์ถœ๋ ฅํ•˜๊ณ , ์•ˆ๋„˜์œผ๋ฉด total์„ ์ถœ๋ ฅํ•ด์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

๐Ÿ“š ์ƒˆ๋กญ๊ฒŒ ์•Œ๊ฒŒ๋œ ๋‚ด์šฉ

@9kyo-hwang
Copy link

์•„๋‹ˆ ์นœ๊ตฌ๋น„ ์งค ๋นตํ„ฐ์กŒ๋„ค ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹

Copy link
Member

@tgyuuAn tgyuuAn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

import sys
from collections import defaultdict

def input(): return sys.stdin.readline().rstrip()

N, M, money = map(int, input().split())
parent_cost = [(idx, cost) for idx, cost in enumerate(map(int, input().split()))]

def find_parent(graph, element):
    if element == graph[element][0]: 
        return graph[element]
    graph[element] = find_parent(graph, graph[element][0])
    return graph[element]

def union(graph, x, y):
    x_parent = find_parent(graph, x)
    y_parent = find_parent(graph, y)
    if x_parent[0] != y_parent[0]:
        if x_parent[1] < y_parent[1]:
            graph[y_parent[0]] = x_parent
        else:
            graph[x_parent[0]] = y_parent

parent = {idx: (idx, cost) for idx, cost in parent_cost}

for _ in range(M):
    fr1, fr2 = map(int, input().split())
    union(parent, fr1-1, fr2-1)
    
visited = set()
need_money = 0

for idx in range(N):
    if idx not in visited:
        stack = [idx]
        min_cost = float('inf')
        
        while stack:
            node = stack.pop()
            if node in visited:
                continue
            visited.add(node)
            min_cost = min(min_cost, parent[node][1])
            parent_node = find_parent(parent, node)[0]
            for next_node in range(N):
                if next_node != node and find_parent(parent, next_node)[0] == parent_node and next_node not in visited:
                    stack.append(next_node)
        
        need_money += min_cost

print(need_money) if need_money <= money else print("Oh no")

๋ฒ„์Šค์• ์„œ ์œ ๋‹ˆ์˜จ ํŒŒ์ธ๋“œ๋กœ ํ’€๋ฆฌ๊ฒ ๋‚œ๋ฐ? ํ•˜๊ณ  ํฐ์ฝ”๋”ฉ์œผ๋กœ ํ’€์—ˆ์”๋‹ˆ๋ง ํžˆํžˆ

์นญ์ฐฌํ•ด์กฐ~~~~~!

image

Copy link

@9kyo-hwang 9kyo-hwang left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์œ ๋‹ˆ์˜จ ํŒŒ์ธ๋“œ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์ตœ์ ํ™”ํ•˜๋Š” ๋ฐฉ๋ฒ•์œผ๋กœ ๋ณดํ†ต ๊ฒฝ๋กœ ์••์ถ•(Path Compreesion) ๊ธฐ๋ฒ•์„ ๋งŽ์ด ์‚ฌ์šฉํ•˜์ฃ . ์ด๊ฒƒ ์™ธ์—๋„ ๊ฐ€์ค‘์น˜ ๊ฒฐํ•ฉ(Weighted Union) ๊ธฐ๋ฒ•๋„ ์žˆ๋Š”๋ฐ์š”, ๊ฐ ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ ์ •๋ณด๋ฅผ ์ด์šฉํ•ด ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ๋ฅผ ๊ท ๋“ฑํ•˜๊ฒŒ ๋งŒ๋“œ๋Š” ์ตœ์ ํ™” ๊ธฐ๋ฒ•์ž…๋‹ˆ๋‹ค(๊ด€๋ จ ์˜์ƒ).

์—ฌ๊ธฐ์„œ ์ด๋ฅผ ์‘์šฉํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ์š”, ์—ฌ๊ธฐ์„œ๋Š” ์ตœ์ ํ™”์˜ ๋ชฉ์ ์œผ๋กœ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  ๋ฌธ์ œ์—์„œ "์นœ๊ตฌ์˜ ์นœ๊ตฌ๋Š” ์นœ๊ตฌ"๋ผ ํ–ˆ์œผ๋‹ˆ, ๊ฐ™์€ ์ง‘ํ•ฉ์— ์žˆ๋Š” ์นœ๊ตฌ๋“ค์„ ์‚ฌ๊ทˆ ๋•Œ๋Š” ๊ทธ ์ค‘ "๊ฐ€์žฅ ์ ์€ ์นœ๊ตฌ๋น„"๋กœ ํ†ต์ผ์‹œ์ผœ๋„ ๋˜๊ฒ ์ฃ . ์ฆ‰ ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ๋ฅผ ๋‚˜ํƒ€๋‚ด์ง€ ์•Š๊ณ  ๊ฐ€์žฅ ์ ์€ ์นœ๊ตฌ๋น„๋ฅผ ๊ธฐ๋กํ•˜๋Š” ์šฉ๋„๋กœ ์‚ฌ์šฉํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.

def main(input):
    N, M, k = map(int, input().split())
    sz = [0] + list(map(int, input().split()))  # ๊ฐ ์นœ๊ตฌ๋ฅผ ์‚ฌ๊ท€๋Š” ๋ฐ ํ•„์š”ํ•œ ๋น„์šฉ
    p = [i for i in range(N + 1)]

    def find(x: int) -> int:
        while x != p[x]:
            p[x] = p[p[x]]
            x = p[x]
        
        return p[x]


    def union(x: int, y: int):
        x = find(x)
        y = find(y)

        if sz[x] > sz[y]:  # ์นœ๊ตฌ๋น„๊ฐ€ ๋” ์ ์€ ์ชฝ์œผ๋กœ ๊ฒฐํ•ฉ
            p[x] = y
            sz[x] = sz[y]
        else:
            p[y] = x
            sz[y] = sz[x]


    for _ in range(M):
        v, w = map(int, input().split())
        union(v, w)

    s = set()  # ๊ฐ ์นœ๊ตฌ ์ง‘ํ•ฉ ๋‹น 1๋ช…์”ฉ๋งŒ ์žˆ์œผ๋ฉด ๋˜๋ฏ€๋กœ set์„ ์ด์šฉํ•ด ๊ธฐ๋ก
    ans = 0
    for friend in range(1, N + 1):
        root = find(friend)  # ๊ฐ ์นœ๊ตฌ ์ง‘๋‹จ์˜ ์ตœ์ƒ์œ„ ๋ถ€๋ชจ๋ฅผ ๊ฐ€์ ธ์˜ด
        if root not in s:  # ์ด์ „์— ๋งŒ๋‚œ ์นœ๊ตฌ ์ง‘ํ•ฉ์ด ์•„๋‹ˆ๋ผ๋ฉด 
            s.add(root)  # ์ด ์ง‘๋‹จ์— ์†ํ•˜๋Š” ์นœ๊ตฌ๋“ค์€ ๋ชจ๋‘ ์‚ฌ๊ท
            ans += sz[root]  # ๊ทธ ์นœ๊ตฌ ์ง‘๋‹จ์˜ ์นœ๊ตฌ๋“ค์„ ์‚ฌ๊ท€๊ธฐ ์œ„ํ•œ ๋น„์šฉ

    print("Oh no" if ans > k else ans)


if __name__ == "__main__":
    main(open(0).readline)

์ด๋Ÿฐ ์‹์ด๋ฉด ๋ณ„๋„๋กœ stack์ด๋‚˜ queue๋ฅผ ์“ธ ํ•„์š” ์—†์ด ๋ฐ˜๋ณต๋ฌธ ํ•œ ๋ฒˆ๋งŒ ๋Œ๋ ค์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค :)

@alstjr7437
Copy link
Member

๋‹ค๋ฅธ ๋ถ„๋“ค์€ ์œ ๋‹ˆ์˜จํŒŒ์ธ๋“œ๋ฅผ ๋งŽ์ด ์“ฐ์…จ๊ตฐ์š”

์ €๋Š” ์•„์ง ์œ ๋‹ˆ์˜จํŒŒ์ธ๋“œ๊ฐ€ ์ต์ˆ™ํ•˜์ง€ ์•Š์•„์„œ ํ™์ฃผ๋‹˜์ด๋ž‘ ๋˜‘๊ฐ™์ด BFS๋ฅผ ์ด์šฉํ•ด์„œ ํ’€์—ˆ์Šต๋‹ˆ๋‹ค!!

๊ทธ๋Ÿฐ๋ฐ resultMoney๋ฅผ ๋ฐ”๋กœ ํ•˜๋Š” ๋ฐฉ๋ฒ•๋„ ์žˆ์—ˆ๋„ค์š”!!

import sys
from collections import deque

input = sys.stdin.readline

N, M, k = map(int, input().split())
student = list(map(int, input().split()))
friend = [[] for i in range(N+1)]
visited = [False] * (N+1)
resultMoney = 0

for _ in range(M) :
    a,b = map(int, input().split())
    friend[a].append(b)
    friend[b].append(a)

def bfs(start):
    queue = deque([start])
    visited[start] = True
    group_min_cost = student[start-1]
    
    while queue:
        node = queue.popleft()
        
        for neighbor in friend[node]:
            if not visited[neighbor]:
                visited[neighbor] = True
                queue.append(neighbor)
                group_min_cost = min(group_min_cost, student[neighbor-1])
    
    return group_min_cost

for i in range(1, N+1):
    if not visited[i]:
        resultMoney += bfs(i)

if resultMoney > k:
    print("Oh no")
else:
    print(resultMoney)

Comment on lines +31 to +33
if friends:
cost = min(friend_money[friend] for friend in friends)
total += cost
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cost๋ฅผ ๋‹ค ์ €์žฅํ•ด๋‘์‹œ๊ณ  ๋งˆ์ง€๋ง‰์— min์œผ๋กœ ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์ด์šฉํ•˜์…จ๊ตฐ์š”!!

์ €๋Š” queue์•ˆ์—์„œ ๋Œ๋ฉด์„œ ๋ฐ”๋กœ๋ฐ”๋กœ ์ œ์ผ ์ž‘์€ ๋ถ€๋ถ„์„ ๋„ฃ์–ด์คฌ๋Š”๋ฐ ์ด ๋ถ€๋ถ„๋„ ์ข‹์•„๋ณด์ด๋„ค์š”!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants